package cn.itsource.pethome.org.service.impl;

import cn.itsource.pethome.basic.service.impl.BaseServiceImpl;
import cn.itsource.pethome.org.constant.Constant;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.domain.Shop;
import cn.itsource.pethome.org.mapper.EmployeeMapper;
import cn.itsource.pethome.org.mapper.ShopMapper;
import cn.itsource.pethome.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * 商家入驻
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 商家入驻
     *
     * @param shop
     */
    @Override
    @Transactional
    public void settledIn(Shop shop) {
        //先进行用户注册,获取传递过来的用户信息
        Employee admin = shop.getEmployeeName();
        //给用户设置转态
        admin.setState(Constant.EMP_AUDIT);
        //商家账号注册
        employeeMapper.save(admin);
        //获取注册的id，将id设置会admin对象
        admin.setId(admin.getId());
        //进行商家入驻注册
        shopMapper.save(shop);
    }

    /**
     * 禁用商家
     * @param id
     */
    @Override
    @Transactional
    public void DisableAudit(Long id) {
        //根据id查询商品信息
        Shop shop = shopMapper.loadById(id);
        //重新设置审核状态信息为禁用
        shop.setState(2);
        //更新到数据库
        shopMapper.update(shop);
        //根据id查询用户信息
        Employee employee = employeeMapper.loadById(shop.getEmployeeName().getId());
        //重新设置审核状态信息为禁用
        employee.setState(0);
        //更新到数据库
        employeeMapper.update(employee);
    }

    /**
     * 商家信息审核通过
     *店铺状态   0 待审核  1  审核通过   -1 审核不通过  2 禁用
     * @param id
     */
    @Override
    @Transactional
    public void AuditPass(Long id) throws MessagingException {
        //根据id查询商品信息
        Shop shop = shopMapper.loadById(id);
        //重新设置审核状态信息为审核通过
        shop.setState(1);
        //更新到数据库
        shopMapper.update(shop);
        //根据id查询用户信息
        Employee employee = employeeMapper.loadById(shop.getEmployeeName().getId());
        //重新设置审核状态信息为审核通过
        employee.setState(2);
        //更新到数据库
        employeeMapper.update(employee);

        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //复杂邮件工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        //发件人
        helper.setFrom("1162443647@qq.com");
        //邮件标题
        helper.setSubject("恭喜您成功入驻宠物之家！");
        //邮件内容
        helper.setText(" <a href=\"http://localhost:8080/#/login\">\n" +
                "      <img src=\"http://121.37.194.36/group1/M00/00/62/wKgABF8yXCGAWQWdAACku36L_j8752.jpg\" width=\"300px\" alt=\"不存在\">\n" +
                "      跳转到登录页面，我们非常高兴您的成功入驻，希望我们的合作能互利互赢，期待和您见面！\n" +
                "    </a>",true);
        //附件
        helper.addAttachment("宠物.jpg", new File("E:\\图片\\6.jpg"));

        //收件人
        helper.setTo("aine1113@163.com");
        //发送邮件
        javaMailSender.send(mimeMessage);
    }

    /**
     * 商家信息审核不通过
     *
     * @param id
     */
    @Override
    public void AuditNoPass(Long id) {
        //根据id查询商品信息
        Shop shop = shopMapper.loadById(id);
        //重新设置审核状态信息为审核通过
        shop.setState(-1);
        //更新到数据库
        shopMapper.update(shop);
        //根据id查询用户信息
        Employee employee = employeeMapper.loadById(shop.getEmployeeName().getId());
        //重新设置审核状态信息为审核通过
        employee.setState(-1);
        //更新到数据库
        employeeMapper.update(employee);
    }

    /**
     * 商店信息审核状态
     *
     * @param id
     * @return
     */
    @Override
    public Shop shopState(Long id) {
        return shopMapper.shopState(id);
    }

    /**
     * 根据登录用户查询商铺信息
     *
     * @param employee
     * @return
     */
    @Override
    public Shop loadByEmployee(Employee employee) {

        return shopMapper.loadByEmployee(employee.getId());
    }


}
